package it.univaq.mwt.facade;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.persistence.PersistenceException;

import it.univaq.mwt.business.ServicesService;
import it.univaq.mwt.exceptionmonitor.LivelloEx;
import it.univaq.mwt.exceptionmonitor.WishListrException;
import it.univaq.mwt.exceptionmonitor.WishlistUserException;
import it.univaq.mwt.exceptionmonitor.WishlistrMessage;
import it.univaq.mwt.log.LivelloLog;
import it.univaq.mwt.log.LogManager;
import it.univaq.mwt.model.bean.ServicesBeanRemote;
import it.univaq.mwt.model.entity.Services;

public class FacadeServices implements ServicesService ,LivelloEx, LivelloLog {

private ServicesBeanRemote sbr;
	

	public FacadeServices(ServicesBeanRemote sbr) {
		super();
		this.sbr = sbr;
	}
	
	
	public Set<Services> getServices(int id_group, int position)  {
		
		Set<Services> services=null;
		try{
			try {
				services=sbr.getServiceByIdGroup(id_group,position);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeService - getServices : " + e.getStackTrace(), ERR);
				// throws
			}
		
			LogManager.getIstance().scrivi("FacadeService - getServices: elenco dei servizi per il gruppo " + id_group, RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}	
		return services;
	}

	
	public void addService(Services s,int[] groups)  {
	
		try {
			try {
			sbr.addService(s,groups);
			
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeUser - addService : " + e.getMessage(), ERR);
				throw new WishlistUserException("Errore nell'aggiunta del servizio");
			}
			
			LogManager.getIstance().scrivi("FacadeService - addService: aggiunta del servizio" + s.getName(), RUN);
		} catch (IOException e) {
				new WishListrException(e,ERROR);
		} catch (PersistenceException e) {
			System.out.println("service non creato");
		} catch (Exception e) {
			System.out.println("service non creato");
		}
	}

	
	public void delete(int id_service) throws WishlistUserException  {
		
		try {			
			try {
				sbr.deleteService(id_service);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeUser - delete : " + e.getMessage(), ERR);
				throw new WishlistUserException("Errore nella cancellazione del servizio");
			}
		
			LogManager.getIstance().scrivi("FacadeService - delete: Cancellazione del servizio con id " + id_service, RUN);
		} catch (IOException e) {
				new WishListrException(e,ERROR);
		} 
	}

	
	public List<Services> getList(int i, int j)  {
		
		List<Services> list = null;
		try {
			try {
				list = sbr.getList(i,j);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeUser - addService : " + e.getMessage(), ERR);
//				throw new WishlistUserException("");
			}
		
			LogManager.getIstance().scrivi("FacadeService - getList: lista dei servizi", RUN);
		} catch (IOException e) {
				new WishListrException(e,ERROR);
		} 
		
		return list;
		
	}

	
	public Services getService(int id)  {
		Services s = null;
		
		try {
			try {
				s = sbr.getService(id);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeService - getService : " + e.getMessage(), ERR);
				//	throw new WishlistUserException("");
			}
		
			LogManager.getIstance().scrivi("FacadeService - getService : Richiesta del servizio con id " + id, RUN);
		} catch (IOException e) {
				new WishListrException(e,ERROR);
		}
		return s;
	}

	
	public int getServicesNumber()  {
		int number=0;
		
		try {
			try {
				number = sbr.getServiceNumber();
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeService - getServicesNumber : " + e.getMessage(), ERR);
				//	throw new WishlistUserException("");
			}
					
			LogManager.getIstance().scrivi("FacadeService - getServicesNumber : Richiesta del numero dei servizi" , RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}
		return number;
	}

	
	public void setEditService(Services s,int[] groups, int id_service)  throws WishlistUserException {

		try {
			try {
				sbr.editService(s,groups,id_service);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeUser - setEditService : " + e.getMessage(), ERR);
				throw new WishlistUserException("Errore nella modifica del servizio");
			}
			
			LogManager.getIstance().scrivi("FacadeService - setEditService: " + s.getName() , RUN);
		} catch (PersistenceException e) {
			new WishListrException(e,ERROR);
		} catch (Exception e) {
			new WishListrException(e,ERROR);
		}
	}
}
